package com.xbongbong.saas.model;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.wrap.IModel;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.domain.dao.SpecificationValueDao;
import com.xbongbong.saas.domain.entity.SpecificationValueEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Program xbb-pro
 * @Description
 * @Author caoyy
 * @Date 2019/9/10
 **/
@Service
public class SpecificationValueModel implements IModel<SpecificationValueEntity> {

    @Resource
    private SpecificationValueDao dao;

    @Override
    public Integer insert(SpecificationValueEntity entity) throws XbbException {
        entity.setAddTime(DateTimeUtil.getInt());
        entity.setUpdateTime(DateTimeUtil.getInt());
        entity.setFrequency(1L);
        return dao.insert(entity);
    }

    @Override
    public Integer update(SpecificationValueEntity entity) throws XbbException {
        entity.setUpdateTime(DateTimeUtil.getInt());
        return dao.update(entity);
    }

    @Override
    public List findEntitys(Map<String, Object> param) {
        return dao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return dao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    public Integer save(SpecificationValueEntity entity) throws XbbException {
        Map params = new HashMap(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("del",0);
        params.put("corpid",entity.getCorpid());
        params.put("specificationKey",entity.getSpecificationKey());
        params.put("specificationValue",entity.getSpecificationValue());
        List<SpecificationValueEntity> specificationValueEntityList = findEntitys(params);
        if (Objects.isNull(specificationValueEntityList) || specificationValueEntityList.isEmpty()){
            return insert(entity);
        } else {
            SpecificationValueEntity specificationValueEntity = specificationValueEntityList.get(0);
            Long frequency = specificationValueEntity.getFrequency();
            specificationValueEntity.setFrequency(++frequency);
            return update(specificationValueEntity);
        }
    }

    public Integer deleteByKey(Long key, String corpid) {
        return dao.deleteByKey(key, corpid);
    }

    public Integer deleteByKeys(List<Long> keys, String corpid) {
        return dao.deleteByKeys(keys, corpid);
    }

    public Integer deleteByProductId(Long productId, String corpid) {
        return dao.deleteByProductId(productId, corpid);
    }

    public void insertBatch(List<SpecificationValueEntity> list) {
        dao.insertBatch(list);
    }

    /**
     * 获取所有的规格
     *
     * @param productId 父产品id
     * @param corpid    公司id
     * @author GuoJun.HU
     * @since 3.14
     * 创建时间 2018/1/15 17:20
     */
    public List<SpecificationValueEntity> getSpecificationList(String corpid, Long productId) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(ParameterConstant.CORPID, corpid);
        param.put(ParameterConstant.DEL, 0);
        param.put("productId", productId);
        param.put("orderByStr", "sort asc");
        return findEntitys(param);
    }

}
